/**
 * <pre>
 * ss-fire-collector / ListNet.java
 * test.rmi Class
 * ListNet
 * <br>
 * 
 * <br>
 * @author ytjeong/apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 20.
 * </pre>
 */
package test.rmi;

/** <pre>
 * Class : test.rmi.ListNet / ListNet.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 20.
 */

import java.io.Console;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;

import collector.core.util.StringUtil;



public class ListNets {
    @SuppressWarnings("unused")
	private static final Console console = System.console();
    private static String tab1 = "\t";
    private static String tab2 = tab1 + tab1;
    @SuppressWarnings("unused")
	private static String tab3 = tab1 + tab2;

    public static void main(String args[]) throws SocketException {
		Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
	
		for (NetworkInterface netint : Collections.list(nets)) {
		    //displayInterfaceInformation(netint);
		    displayInterfaceInformationDetail(netint);
		}
    }

    @SuppressWarnings("unused")
	private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
		System.out.printf("Display name: %s%n", netint.getDisplayName());
		System.out.printf("Name: %s%n", netint.getName());
	
		Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
		for (InetAddress inetAddress : Collections.list(inetAddresses)) {
		    System.out.printf(tab1 + "==> InetAddress: %s%n", inetAddress);
		}
	
		System.out.printf("%n");
    }

    public static String ConvCharSet (String src, String from, String to) {
		String res = src;
	
		try {
		    if ( to == null || to.equals("") ) res = new String(src.getBytes(from));
		    else res = new String(src.getBytes(from),to);
		} catch (UnsupportedEncodingException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		
		return res;
    }

    public static void charsetFind(String s) {
		try {
		    String [] charset = {"EUC-KR","KSC5601","ISO-8859-1","UTF-8","ASCII"};
		    for (int i1=0;i1<charset.length;i1++) {
				for (int i2=0;i2<charset.length;i2++) {
				    if ( i1 == i2 ) continue;
				    System.out.print( "\t"+ "str[" + s + "]" + charset[i1] + "->" + charset[i2] + ":" );
				    System.out.println( new String(s.getBytes(charset[i1]),charset[i2]) );
				}
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		}
    }
    public static String ConvTestCharSet (String src) {
		String res = src;
		String from = "";
		String to = "";
	
		to = ""; //ISO-8859-1
		from = "UTF-8"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "EUC-KR"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "ISO-8859-1"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		
		to = "ISO-8859-1"; //
		from = "UTF-8"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "EUC-KR"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "ISO-8859-1"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		
		to = "EUC-KR"; //
		from = "UTF-8"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "EUC-KR"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "ISO-8859-1"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		
		to = "UTF-8"; //
		from = "UTF-8"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "EUC-KR"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		from = "ISO-8859-1"; 
		System.out.println("\tConv from="+from+" to="+to+" :: "+ConvCharSet(src, from, to));
		
		return res;
    }
    
    @SuppressWarnings("unused")
	private static void displayInterfaceInformationDetail(NetworkInterface netint) throws SocketException {
		NetworkInterface lparent = netint.getParent();
		boolean isUp = netint.isUp();
		boolean isLoopback = netint.isLoopback();
		String dispName = netint.getDisplayName();
		dispName = ConvCharSet(dispName, "ISO-8859-1", "EUC-KR");// :: Bluetooth 장?(개인 영역
		
		//ConvTestCharSet(dispName);
		//charsetFind(dispName);
		//ConvTestCharSet(dispName);
       	if (isUp) {
       	    	System.out.printf("Display name: %s : %s%n", netint.getDisplayName(), dispName );
        	System.out.printf("Name: %s%n", netint.getName());
        
        	Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        	for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        	    System.out.printf(tab1 + "==> InetAddress: %s%n", inetAddress);
        	}
        
        	System.out.printf(tab2 + "is Parent: %s%n", netint.getParent());
        	System.out.printf(tab2 + "is Up? %s%n", netint.isUp());
        	System.out.printf(tab2 + "is Loopback? %s%n", netint.isLoopback());
        	System.out.printf(tab2 + "is PointToPoint? %s%n", netint.isPointToPoint());
        	System.out.printf(tab2 + "Supports multicast? %s%n", netint.supportsMulticast());
        	System.out.printf(tab2 + "is Virtual? %s%n", netint.isVirtual());
        	System.out.printf(tab2 + "Hardware address: %s%n", Arrays.toString(netint.getHardwareAddress()));
        	System.out.printf(tab2 + "Hardware address: %s%n", StringUtil.toHexString(netint.getHardwareAddress()));
        	
        	System.out.printf(tab2 + "MTU: %s%n", netint.getMTU());

        	System.out.printf(tab2 + "netint Str: %s%n", netint.toString());
        
//        	List<InterfaceAddress> interfaceAddresses = netint
//        		.getInterfaceAddresses();
//        	for (InterfaceAddress addr : interfaceAddresses) {
//        	    System.out.printf(tab2 + "==> InterfaceAddress: %s%n", addr.getAddress());
//        	}
//        	System.out.printf("%n");
        	Enumeration<NetworkInterface> subInterfaces = netint.getSubInterfaces();
        	for (NetworkInterface networkInterface : Collections
        		.list(subInterfaces)) {
        	    System.out.printf(tab2 + "%nSubInterface%n");
        	    displayInterfaceInformationDetail(networkInterface);
        	}
        	System.out.printf("%n");
       	}
    }
}